<?php

namespace app\admin\controller\user;

use app\common\controller\Backend;
use app\common\model\Funds;
use think\Db;
use think\Exception;

/**
 * 会员管理
 *
 * @icon fa fa-user
 */
class User extends Backend
{


	protected $relationSearch = true;

    /**
     * @var \app\admin\model\User
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('User');
        $list = [
        	'levelList' => \app\admin\model\User::$levelList
        ];
        $this->assign($list);
        $this->assignconfig($list);
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $with = ['recommend'];
            
            $total = $this->model
	            ->with($with)
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
	            ->with($with)
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as $k => $v) {
                $v->hidden(['password', 'salt']);
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $row = $this->model->get($ids);
        if (!$row)
            $this->error(__('No Results were found'));
//        $this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker']));
        return parent::edit($ids);
    }

    public function recharge($ids) {
    	
    	if($this->request->isPost()) {
		    $row = $this->model->get($ids);
		    $params = $this->request->post("row/a");
		    
		    $this->model->startTrans();
		    try {
		    	$fundsModel = new Funds();
		    	$where['id'] = $ids;
			    if($params['money'] > 0 ) {
			    	$moneyRes = Db::name('user')->where($where)->setInc('money', $params['money']);
			    	if(!$moneyRes) {
			    		throw new Exception('添加余额出错');
				    }
			    	
			    	$fundsRes = $fundsModel->addRecord($ids, abs($params['money']), $row['money'], $fundsModel::ACCOUNT_TYPE_BALANCE, $params['money'] > 0 ? $fundsModel::OPE_TYPE_PLUS : $fundsModel::OPE_TYPE_LESS, $fundsModel::TYPE_ADMIN_OPERATE, '', $params['remark']);
			    	if(!$fundsRes) {
			    		throw new Exception('添加余额流水出错');
				    }
			    }
			
			    if($params['score'] > 0 ) {
				    $scoreRes = Db::name('user')->where($where)->setInc('score', $params['score']);
				    if(!$scoreRes) {
					    throw new Exception('添加积分出错');
				    }
				
				    $fundsRes = $fundsModel->addRecord($ids, abs($params['score']), $row['score'], $fundsModel::ACCOUNT_TYPE_INTEGRAL, $params['score'] > 0 ? $fundsModel::OPE_TYPE_PLUS : $fundsModel::OPE_TYPE_LESS, $fundsModel::TYPE_ADMIN_OPERATE, '', $params['remark']);
				    if(!$fundsRes) {
					    throw new Exception('添加积分流水出错');
				    }
			    }
		    	
		    	$this->model->commit();
		    	$this->success();
		    } catch (Exception $e) {
		    	$this->model->rollback();
		    	$this->error($e->getMessage());
		    }
	    }
	    
	    return $this->view->fetch();
    }
}
